'use strict';
// 商户设置视图模块

// 视图初始化
var $iosDialog1, $staffAddBox;

function viewInit() {
    initValue();
    addyiyeyuanEvent();
    cancelAddEvent();
    $("#change_phone_btn").on("touchend", function() {
        setTimeout(function() {
            location.href = "./changePhone.html?openid=" + openId;
        }, 200);
    });
    $("#change_pw_btn").on("touchend", function() {
        setTimeout(function() {
            location.href = "./changePassword.html?openid=" + openId;
        }, 200);
    });
    $("#fanhui").on("touchstart", function() {
        location.href = "./merchantHome.html";
    });
    $("#delete-no").on("touchend", hidedeleteDialog);
    $("#staff_phone").on("input", function() {
        var sval = $(this).val();
        if(sval.length >11){
            $(this).val(sval.substr(0,11));
        }
        if(sval.length > 10){
            $(this).blur();
        }
    });
}

// 初始化视图模块变量
function initValue() {
    $iosDialog1 = $("#addDialog");
    $staffAddBox = $("#staffAddBox");
}

// 清空添加营业员输入框
function emptyStaffInput() {
    $("#staffName").val("");
    $("#staff_phone").val("");
}

// 添加收银员
function addyiyeyuanEvent() {
    $('#addsyy').on('click', function() {
        $iosDialog1.fadeIn(200);
    });
}

// 取消收银员添加
function cancelAddEvent() {
    $("#cancelAdd").on("click", function() {
        setTimeout(function() {
            $iosDialog1.fadeOut(200);
        }, 200);

    });
}

// 创建一个收银员html
function createOneStaffHtml(oneInfo) {
    var oneStaffHtml = `
    <div class="weui-cell staff_box">
        <div class="weui-cell__hd">
            <span>${oneInfo.no}</span><span>${oneInfo.name}</span>
        </div>
        <div class="weui-cell__bd">
            <span>${oneInfo.mobile}</span>
        </div>
        <div class="weui-cell__ft">
            <a data-cashno=${oneInfo.cashno} data-mobile=${oneInfo.mobile} href="javascript:;" class="weui-btn weui-btn_plain-primary operable ui-link">删除</a>
        </div>
    </div>
    `

    $(oneStaffHtml).insertBefore("#staffAddBox");

}

// 显示loading
function showLoadingDialog() {
    $("#loadingDialog").css({
        "opacity": "1",
        "display": ""
    });
}

// 显示错误提示2
function showdeleteDialog(erroMsg) {
    $("#delete-dialog").css({
        "opacity": "1",
        "display": ""
    });

    //    $("#errorDialog2 .weui-dialog__bd").text(erroMsg);
}

// 隐藏错误提示2
function hidedeleteDialog(erroMsg) {
    setTimeout(function() {
        $("#delete-dialog").fadeOut(200);
    }, 200);


    //    $("#errorDialog2 .weui-dialog__bd").text(erroMsg);
}

// 隐藏loading
function hideLoadingDialog() {
    $("#loadingDialog").fadeOut(200);
}

export {
    viewInit,
    createOneStaffHtml,
    emptyStaffInput,
    showLoadingDialog,
    hideLoadingDialog,
    showdeleteDialog,
    hidedeleteDialog
}